﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using yichu_front;

namespace yichu_manager
{
   public static class MaterialService
    {
        private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
        /// <summary>
        /// 根据点击的具体材料查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static List<Material> getMateriaById(int id)
        {
            List<Material> materials = new List<Material>();
            //根据Id获取具体材料
            Material material = SqlSugarHelp.scope.Queryable<Material>().First(it => it.id == id);
            //计算材料单价+偏移量
            decimal pianYi = 0;
            if (material != null)
            {
                //如果偏移量是数字
                if (decimal.TryParse(material.偏移量, out pianYi))
                {
                    material.材料单价 += pianYi/1000;
                }
                materials.Add(material);
            }
            return materials;
        }
        /// <summary>
        /// 根据点击的分类ID查询所有材料
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public static List<Material> getMateriaByParentId(int parentId)
        {
            List<Material> materials = SqlSugarHelp.scope.Queryable<Material>().Where(it => it.parentId == parentId).ToList();
            return materials;
        }
        /// <summary>
        /// 根据节点ID递归删除子节点
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int deleteMateriaTypeById(int id)
        {
            //先根据ID查询所有的子分类
            List<MaterialType> materialTypes = getChildMaterialTypeByParentId(id);
            foreach (MaterialType item in materialTypes)
            {
                deleteMateriaTypeById(item.id);
            }
           return SqlSugarHelp.scope.Deleteable<MaterialType>().In(id).ExecuteCommand();
        }
        /// <summary>
        /// 根据类型的parentId查询parentId下的所有子节点
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public static List<MaterialType> getChildMaterialTypeByParentId(int parentId)
        {
            return SqlSugarHelp.scope.Queryable<MaterialType>().Where(it => it.parentId == parentId).ToList();
        }
        /// <summary>
        /// 新增分类，并返回自增ID
        /// </summary>
        /// <param name="materialType"></param>
        /// <returns></returns>
        public static int insertMaterialType(MaterialType materialType)
        {
            return SqlSugarHelp.scope.Insertable(materialType).ExecuteReturnIdentity();
        }
        /// <summary>
        /// 新增一条材料
        /// </summary>
        /// <param name="material"></param>
        /// <returns></returns>
        public static int insertMaterial(Material material)
        {
            material.创建时间 = DateTime.Now;
            return SqlSugarHelp.scope.Insertable(material).ExecuteCommand();
        }
        /// <summary>
        /// 新增多条材料
        /// </summary>
        /// <param name="materials"></param>
        /// <returns></returns>
        public static int insertMaterials(List<Material> materials) 
        {
            foreach (Material item in materials)
            {
                item.创建时间 = DateTime.Now;
            }
            return SqlSugarHelp.scope.Insertable(materials).ExecuteCommand();
        }

        public static List<Material> beforeShowMaterialPrice(List<Material> materials)
        {
            foreach (Material item in materials)
            {
                decimal pianYi = 0;
                //不是铜价且偏移量不是字符，要加偏移量
                if (item.材料名称 != "实时铜价" && decimal.TryParse(item.偏移量,out pianYi))
                {
                    item.材料单价 += pianYi/1000;
                }
            }
            return materials;
        }

        //更新材料单价
        public static void updateMaterial(Material material)
        {
            try
            {
                SqlSugarHelp.scope.BeginTran();
                SqlSugarHelp.scope.Updateable(material).ExecuteCommand();
                MaterialPriceHistory materialPriceHistory = new MaterialPriceHistory();
                materialPriceHistory.类别 = material.类别;
                materialPriceHistory.parentId = material.parentId;
                materialPriceHistory.序号 = material.序号;
                materialPriceHistory.材料名称 = material.材料名称;
                materialPriceHistory.材料单价 = material.材料单价;
                materialPriceHistory.偏移量 = material.偏移量;
                materialPriceHistory.备注 = material.备注;
                materialPriceHistory.创建人 = material.创建人;
                materialPriceHistory.修改人 = Pub.username;
                materialPriceHistory.创建时间 = material.创建时间;
                materialPriceHistory.修改时间 = DateTime.Now;
                SqlSugarHelp.scope.Insertable(materialPriceHistory).ExecuteCommand();
                SqlSugarHelp.scope.CommitTran();
            }
            catch (Exception)
            {
                SqlSugarHelp.scope.RollbackTran();
               
            }
        }
    }
}
